#!/bin/bash
#description install php
#data 2017/11/10
#author 51yuki
#source function library

. /etc/rc.d/init.d/functions

function init_centos6() {
#configure yum source on centos6

# Install the software required before compiling
yum -y install vim wget unzip cmake pcre pcre-devel readline-devel zlib-devel openssl-devel openssl-clients  gcc-c++ gcc boost boost-devel bison bison-devel tree
# Temporarily close the firewall and selinux
sed -ir 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config && setenforce 0
}
#configure yum source on centos7
function init_centos7() {
cd /etc/yum.repos.d/ && [ -f Centos-7.repo ] || wget -O Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo && [ -f epel-7.repo ] || wget -O epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

# Install the software required before compiling
yum -y install vim wget lbzip2 unzip cmake pcre pcre-devel readline-devel zlib-devel openssl-devel openssl-clients  gcc-c++ gcc boost boost-devel bison bison-devel tree net-tools
# Temporarily close the firewall and selinux
sed -ir 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config && setenforce 0
systemctl disable firewalld  &>/dev/null
systemctl stop firewalld
# create directory storage software
[ -d /usr/local/src ] || mkdir -p /usr/local/src && echo "the file directory is esxit" 
}
software=php-$1
if [ $# -ne 1 ];then
echo "Usage $0 version <5.6.9>"
exit 1
fi
function install_php_on_centos6(){
	init_centos6
	#如果想让编译的php支持mcrypt、mhash扩展和libevent、图形相关rpm包、支持xml的相关包
	yum -y install libmcrypt libmcrypt-devel mhash mhash-devel gd gd-devel php-mcrypt libevent libevent-devel libjpeg-devel libpng-devel freetype-devel libxml2 libxml2-devel bzip2-devel libcurl-devel  
	SoftDir=/usr/local/src
	#download php 下载位置http://mirrors.sohu.com/php/
	cd $SoftDir && wget http://mirrors.sohu.com/php/${software}.tar.gz && tar xf ${software}.tar.gz && cd $software 
	 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir && make && make install
	  [ $? -eq 0 ] && echo "install php is success"
	#configure php
	cd $SoftDir/$software
	cp php.ini-production /usr/local/$software/etc/php.ini
	cp /usr/local/$software/etc/php-fpm.conf.default /usr/local/$software/etc/php-fpm.conf
	cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm
	if [ "`netstat -lnt|grep 9000|awk -F "[ :]+" '{print $4}'`" != "9000" ];then
             service php-fpm start
             action "php startup success" /bin/true
        else
             action "php is running" /bin/false
        fi

}

function install_php_on_centos7(){
	init_centos7
        #如果想让编译的php支持mcrypt、mhash扩展和libevent、图形相关rpm包、支持xml的相关包
	        yum -y install libmcrypt libmcrypt-devel mhash mhash-devel gd gd-devel php-mcrypt libevent libevent-devel libjpeg-devel libpng-devel freetype-devel libxml2 libxml2-devel bzip2-devel libcurl-devel
        SoftDir=/usr/local/src
        #download php 下载位置http://mirrors.sohu.com/php/
        cd $SoftDir && wget http://mirrors.sohu.com/php/${software}.tar.gz && tar xf ${software}.tar.gz && cd $software
         ./configure --prefix=/usr/local/$software --with-config-file-path=/usr/local/$software/etc --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir && make && make install
          [ $? -eq 0 ] && echo "install php is success"
        #configure php
        cd $SoftDir/$software
        cp php.ini-production /usr/local/$software/etc/php.ini
        cp /usr/local/$software/etc/php-fpm.conf.default /usr/local/$software/etc/php-fpm.conf
        cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm
cat >/usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]

Description=php

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/$software/sbin/php-fpm

ExecStop=/bin/pkill -9 php-fpm

PrivateTmp=true

[Install]

WantedBy=multi-user.target
EOF
 if [ "`netstat -lnt|grep 9000|awk -F "[ :]+" '{print $4}'`" != "9000" ];then
	     systemctl enable php-fpm.service
	     systemctl start php-fpm.service
             action "php startup success" /bin/true
        else
             action "php is running" /bin/false
 fi
	
}

echo "##############################################################################"
        echo -e "\033[0;32m1、Compile the installation php on centos6\033[0m"
        echo -e "\033[0;32m2、Compile the installation php on centos7\033[0m"
echo "#############################################################################"
echo
echo
read -p "Please select the version to install: " NUM
case $NUM in
1)
  install_php_on_centos6;  
   ;;
2)
 install_php_on_centos7;
 ;;
*)
 echo "Please input number"
 exit 2
esac

